/*
 * Created on Mar 20, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package business.productsubsystem;

import java.util.List;
import java.util.logging.Logger;

import middleware.exceptions.DatabaseException;
import business.DbClassQuantity;
import business.Quantity;
import business.exceptions.BackendException;
import business.externalinterfaces.Product;
import business.externalinterfaces.ProductFromGui;
import business.externalinterfaces.ProductSubsystem;
import business.util.TwoKeyHashMap;

public class ProductSubsystemFacade implements ProductSubsystem {
	private static final Logger LOG = Logger.getLogger(ProductSubsystemFacade.class
			.getPackage().getName());
	final String DEFAULT_PROD_DESCRIPTION="New Product";
	CatalogTypesImpl types;
	
    public TwoKeyHashMap<Integer,String,Product> getProductTable() throws BackendException {
    	try {
    		DbClassProduct dbClass = new DbClassProduct();
    		return dbClass.readProductTable();
    	} catch(DatabaseException e) {
    		throw new BackendException(e);
    	}
        
    }
	public TwoKeyHashMap<Integer,String,Product> refreshProductTable() throws BackendException {
		try {
			DbClassProduct dbClass = new DbClassProduct();
			return dbClass.refreshProductTable();	
		} catch(DatabaseException e) {
    		throw new BackendException(e);
    	}
	}

    public List<String> refreshCatalogNames() throws BackendException {
    	try {
    		DbClassCatalogTypes dbclass = new DbClassCatalogTypes();
    		types = dbclass.getCatalogTypes();
    		return types.getCatalogNames();
    	} catch(DatabaseException e) {
    		throw new BackendException(e);
    	}
    }
    
    public List<String> getCatalogNames() throws BackendException {
        try {
        	//don't hit database if not needed
        	if(types == null){
        		DbClassCatalogTypes dbclass = new DbClassCatalogTypes();
        		types = dbclass.getCatalogTypes();
        	}
        	return types.getCatalogNames();
        } catch(DatabaseException e) {
    		throw new BackendException(e);
    	}
    }
	public List<Product> getProductList(String catType) throws BackendException {
		try {
			DbClassProduct dbclass = new DbClassProduct();
			Integer catId = getCatalogIdFromType(catType);
			List<Product> list = dbclass.readProductList(catId);
			//this is a read-only list
			LOG.info("ProductSubsystemFacade.getProductList: returning list");
			return list;
		} catch(DatabaseException e) {
    		throw new BackendException(e);
    	}
	}
	public List<Product> refreshProductList(String catType) throws BackendException {
		try {
			DbClassProduct dbclass = new DbClassProduct();
			Integer catId = getCatalogIdFromType(catType);
			List<Product> list = dbclass.refreshProductList(catId);
			//this is a read-only list
			return list;
		} catch(DatabaseException e) {
			throw new BackendException(e);
		}
		
	}
	public Product getProduct(String prodName) throws BackendException {
		try {
			DbClassProduct dbclass = new DbClassProduct();
			return dbclass.readProduct(getProductIdFromName(prodName));
		} catch(DatabaseException e) {
			throw new BackendException(e);
		}	
	}
	public Product getProductFromId(Integer prodId) throws BackendException {
		return new ProductImpl("","","","");
	}
	public Integer getProductIdFromName(String prodName) throws BackendException {
		//implement		
		return new Integer(1);
		
	}
	
	
	public Integer getCatalogIdFromType(String catType) throws BackendException {
		try {
			DbClassCatalogTypes dbclass = new DbClassCatalogTypes();
			CatalogTypesImpl types = dbclass.getCatalogTypes();
			return types.getCatalogId(catType);
		} catch(DatabaseException e) {
			throw new BackendException(e);
		}
	}
	public ProductFromGui createProduct(String name, String date, String numAvail, String unitPrice) {
		return new ProductImpl(name,date,numAvail,unitPrice);
		
	}
	
	
	/* reads quantity avail and stores in the Quantity argument */
	public void readQuantityAvailable(String prodName, Quantity quantity) throws BackendException {
		//stub
		quantity.setQuantityAvailable("0");
		//implement
				
	}
	@Override
	public void deleteProduct(ProductFromGui product) throws BackendException {
		// new
		
	}
	@Override
	public void deleteCatalog(String catalogName) throws BackendException {
		// new
		
	}
	public void saveNewCatalogName(String name) throws BackendException {
		throw new BackendException("saveNewCatalogName has not been implemented.");
		
	}
	/** the input product has only the values given from the
	 *  gui -- first step is to locate other values needed, and then
	 *  do the save
	 *  database columns:
	 *  productid, productname, totalquantity, priceperunit,
	 *  mfgdate, catalogid, description
	 */
	public void saveNewProduct(ProductFromGui product, String catalogType) throws BackendException {
		try {
			//get catalogid
			Integer catalogid = getCatalogIdFromType(catalogType); 
				//invent description
			String description = DEFAULT_PROD_DESCRIPTION;
			DbClassProduct dbclass = new DbClassProduct();
			dbclass.saveNewProduct(product, catalogid,description);
		} catch(DatabaseException e) {
			throw new BackendException(e);
		}
		
	}

}
